package java44th.group.service.admin.history;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketSearchHistory;
import java44th.group.bean.po.MarketSearchHistoryExample;
import java44th.group.mapper.MarketSearchHistoryMapper;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/12 21:05
 */
@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public CommonData<MarketSearchHistory> list(BasePageInfo basePageInfo, Integer userId, String keyword) {

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        example.getOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(keyword)){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(example);

        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(marketSearchHistories);

        return CommonData.data(marketSearchHistoryPageInfo);
    }
}
